package com.gxuwz.controller;


import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gxuwz.constant.ConfigConsts;
import com.gxuwz.entity.Material;
import com.gxuwz.entity.Purchase;
import com.gxuwz.service.IMaterialService;
import com.gxuwz.service.IPurchaseService;

/**
 * <p>
 *  采购单前端控制器
 * </p>
 *
 * @author 刘玉霜
 * @since 2017-10-27
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController implements ICurdController<Purchase>{

	private static Logger log = Logger.getLogger(PurchaseController.class);
	
	private Map<String,Object> json = null;
	
	@Autowired
	private IPurchaseService purchaseService;
	@Autowired
	private IMaterialService materialService;
	
	public PurchaseController(){
		json = new HashMap<String,Object>();
	}
	
	@RequestMapping(value="/create")
	public String create(Model model){
		Purchase p = purchaseService.createPurchase();
		model.addAttribute("purchase", p);
		Page<Material> page = new Page<Material>(1,ConfigConsts.PAGE_SIZE);
		page = materialService.selectPage(page,
				new EntityWrapper<Material>()
				.eq("purchase_id", p.getId()));		
		model.addAttribute("page",page);
		return "purchase/create-purchase";
	}
	
	@Override
	public String openAddView() {
		return null;
	}

	@Override
	public Map<String, Object> add(Purchase t, Model model) {
		log.info("********** execute save user method **********");
		if(null == t){
			json.put("code", -1);
			return json;
		}	
		json.put("code", purchaseService.modify(t));
		return json;
	}

	@Override
	public String list(Model model,Purchase p) {
		if(null == p){
			p = new Purchase();
		}
		Page<Purchase> page = new Page<Purchase>(1,ConfigConsts.PAGE_SIZE);
		page = purchaseService.selectWithUser(page, p);	
		model.addAttribute("page",page);
		return "purchase/list-purchase";
	}

	@Override
	public Map<String, Object> delete(Purchase t) {

		return null;
	}

	@Override
	public String openUpdateView(String id, Model model) {

		return null;
	}

	@Override
	public Map<String, Object> update(Purchase t) {
		return null;
	}
	
}
